<?php

namespace Live\Controller;
require_once 'Qiniu/autoload.php';
use \Qiniu\Auth;
use \Qiniu\Storage\UploadManager;
use Live\Controller\CommonController;

class ColumnController extends CommonController{

    public function column_list(){
        $where['delete_time']=0;

        $columnList=M('FrontCategory')->where($where)->order('status desc,sort asc,id desc')->select();
        $this->assign('columnList',$columnList);
        $this->display();
    }

    public function columnSetting(){
        $id=I('get.id');
        if($id){
            $cateInfo=M('FrontCategory')->find($id);
            if($cateInfo){
                $cateTypeList=M('FrontCategoryType')->select();
                //七牛对象存储的上传凭证
                $putPolicy=array(
                    'saveKey'=>'columnList/$(etag)',
                );
                $upAuth = new Auth(C('QINIU_accessKey'), C('QINIU_secretKey'));
                $bucket = C('QINIU_BUCKET');
                $uploadToken = $upAuth->uploadToken($bucket,null, 3600,$putPolicy);


                $this->assign('uploadToken', $uploadToken);
                $this->assign('cateInfo',$cateInfo);
                $this->assign('cateTypeList',$cateTypeList);
                $this->display();
            }else{
                $this->error('栏目信息错误',U('column_list'));
            }
        }else{
            $this->error('参数错误',U('column_list'));
        }

    }

    public function updateColumnSort(){
        $ids=I('post.ids');
        if($ids){
            $ids=explode(",",$ids);
            $time=time();
            M()->startTrans();
            foreach($ids as $k=>$v){
                $res=M('FrontCategory')->save(array('id'=>$v,'sort'=>$k,'update_time'=>$time));
                $mess['sql'][]=$res.":".M()->_sql();
                if($res===false){
                    M()->rollback();
                    $mess['resultCode']=1;
                    $mess['info']='保存失败';

                    $this->ajaxReturn($mess);
                    eixt();
                }
            }
            M()->commit();
            $mess['resultCode']=0;
            $mess['info']='';
        }else{
            $mess['resultCode']=1;
            $mess['info']='参数错误';
        }
        $this->ajaxReturn($mess);
    }

    public function saveColumnSet(){
        if(IS_POST){
            $data=I('post.');
            if(!isset($data['is_category_show'])){
                $data['is_category_show']=0;
            }
            if(!isset($data['is_category_more'])){
                $data['is_category_more']=0;
            }
            if(!isset($data['status'])){
                $data['status']=0;
            }
            $data['update_time']=time();
            foreach($data as $k=>$v){
                if($v===''){
                    unset($data[$k]);
                }
            }
            $res=M('FrontCategory')->save($data);
            if($res){
                $this->success("修改成功",U('column_list'));
            }else{
                $this->error('修改失败，稍后重试');
            }
        }

    }

    public function addNewColumn(){
        if(IS_POST){
            $data=I('post.');
            if(!isset($data['is_category_show'])){
                $data['is_category_show']=0;
            }
            if(!isset($data['is_category_more'])){
                $data['is_category_more']=0;
            }
            $data['create_time']=$data['update_time']=time();
            foreach($data as $k=>$v){
                if($v===''){
                    unset($data[$k]);
                }
            }
            $res=M('FrontCategory')->add($data);
            if($res){
                $this->success("修改成功",U('column_list'));
            }else{
                $this->error('修改失败，稍后重试');
            }
        }else{
            $cateTypeList=M('FrontCategoryType')->select();
            //七牛对象存储的上传凭证
            $putPolicy=array(
                'saveKey'=>'columnList/$(etag)',
            );
            $upAuth = new Auth(C('QINIU_accessKey'), C('QINIU_secretKey'));
            $bucket = C('QINIU_BUCKET');
            $uploadToken = $upAuth->uploadToken($bucket,null, 3600,$putPolicy);


            $this->assign('uploadToken', $uploadToken);
            $this->assign('cateTypeList',$cateTypeList);
            $this->display();
        }
    }

    public function delColumn(){
        $id=I('get.id');
        if($id){
            $data['delete_time']=time();
            $data['id']=$id;
            $res=M('FrontCategory')->save($data);
            if($res){
                $mess['resultCode']=0;
                $mess['info']='删除成功';
            }else{
                $mess['resultCode']=1;
                $mess['info']='删除失败，稍后重试';
            }
        }else{
            $mess['resultCode']=1;
            $mess['info']='参数错误';
        }
        $this->ajaxReturn($mess);
    }
}